package bb.lanxing.adapter.mine;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import bb.lanxing.R;

public class EditCourseAdapter extends RecyclerView.Adapter<EditCourseAdapter.ViewHolder> {
    private List<String> mDataList;
    private OnItemClickListener listener;
    private Context mContext;

    public EditCourseAdapter(List<String> list) {
        mDataList = list;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layout;
        View head;
        TextView tv_step_number;
        TextView tv_step_name;
        TextView tv_duration;
        LinearLayout ll_target_type1;
        TextView tv_target_type1;
        TextView tv_target_value1;
        ImageView iv_drag;
        ImageView iv_del;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = itemView.findViewById(R.id.layout);
            head = itemView.findViewById(R.id.head);
            tv_step_number = itemView.findViewById(R.id.tv_step_number);
            tv_step_name = itemView.findViewById(R.id.tv_step_name);
            tv_duration = itemView.findViewById(R.id.tv_duration);
            ll_target_type1 = itemView.findViewById(R.id.ll_target_type1);
            tv_target_type1 = itemView.findViewById(R.id.tv_target_type1);
            tv_target_value1 = itemView.findViewById(R.id.tv_target_value1);
            iv_drag = itemView.findViewById(R.id.iv_drag);
            iv_del = itemView.findViewById(R.id.iv_del);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_edit_course, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (mDataList.get(position) == "1") {
            holder.layout.setBackgroundColor(mContext.getColor(R.color.color_warm_up_light));
            holder.head.setBackgroundColor(mContext.getColor(R.color.color_warm_up));
//            head = itemView.findViewById(R.id.head);
//            tv_step_number = itemView.findViewById(R.id.tv_step_number);
//            tv_step_name = itemView.findViewById(R.id.tv_step_name);
//            tv_duration = itemView.findViewById(R.id.tv_duration);
//            ll_target_type1 = itemView.findViewById(R.id.ll_target_type1);
//            tv_target_type1 = itemView.findViewById(R.id.tv_target_type1);
//            tv_target_value1 = itemView.findViewById(R.id.tv_target_value1);
//            iv_drag = itemView.findViewById(R.id.iv_drag);
//            iv_del = itemView.findViewById(R.id.iv_del);
        } else if (mDataList.get(position) == "2") {
            holder.layout.setBackgroundColor(mContext.getColor(R.color.color_active_light));
            holder.head.setBackgroundColor(mContext.getColor(R.color.color_active));
            holder.tv_step_number.setTextColor(mContext.getColor(R.color.color_active));
            holder.tv_step_name.setText("骑车");
            holder.tv_step_name.setTextColor(mContext.getColor(R.color.color_active));
            holder.tv_duration.setTextColor(mContext.getColor(R.color.color_active));
            holder.ll_target_type1.setVisibility(View.GONE);
            holder.tv_target_type1.setTextColor(mContext.getColor(R.color.color_active));
            holder.tv_target_value1.setTextColor(mContext.getColor(R.color.color_active));
            holder.iv_drag.setImageResource(R.drawable.icon_drag_red);
            holder.iv_del.setImageResource(R.drawable.icon_clear_red);
        } else if (mDataList.get(position) == "3") {
            holder.layout.setBackgroundColor(mContext.getColor(R.color.color_cool_down_light));
            holder.head.setBackgroundColor(mContext.getColor(R.color.color_cool_down));
            holder.tv_step_number.setTextColor(mContext.getColor(R.color.color_cool_down));
            holder.tv_step_name.setText("冷身");
            holder.tv_step_name.setTextColor(mContext.getColor(R.color.color_cool_down));
            holder.tv_duration.setTextColor(mContext.getColor(R.color.color_cool_down));
            holder.ll_target_type1.setVisibility(View.GONE);
            holder.tv_target_type1.setTextColor(mContext.getColor(R.color.color_cool_down));
            holder.tv_target_value1.setTextColor(mContext.getColor(R.color.color_cool_down));
            holder.iv_drag.setImageResource(R.drawable.icon_drag_gray);
            holder.iv_del.setImageResource(R.drawable.icon_clear_gray);
        }
        holder.tv_step_number.setText(mDataList.get(position));

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}